package com.example.keli.ui.my.love;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.keli.R;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.BaseFragment;
import com.example.keli.base.utils.DensityUtil;
import com.example.keli.ui.my.adapter.LoveSendAdapter;
import com.example.keli.ui.my.model.LoveSendBean;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

/**
 * 心动
 * description 我喜欢的
 * created by jerry on 2019/3/18.
 */
public class LoveSendFragment extends BaseFragment {

    private static final String TAG = "CommissionFragment";
    @BindView(R.id.recycler_star)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout mRefreshlayout;
    private LoveSendAdapter mAdapter;
    private List<LoveSendBean.UserInfoBean> mListData;
    private int page = 1;

    public static LoveSendFragment newInstance() {
        LoveSendFragment fragment = new LoveSendFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_star;
    }

    @Override
    protected void bindData() {
        initRecycleView();
        loadDatas();
    }

    private void initRecycleView() {
        mListData = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new LoveSendAdapter(mListData, getContext());
        recyclerView.setAdapter(mAdapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .color(ContextCompat.getColor(getActivity(), R.color.divider_line))
                .size(DensityUtil.dip2px(getActivity(), 1))
                .build());

        View emptyView = View.inflate(getContext(), R.layout.empty_view, null);
        TextView text = emptyView.findViewById(R.id.tv_empty);
        text.setText("暂无记录");
        mAdapter.setEmptyView(emptyView);
    }

    @Override
    protected void bindListener() {
        mRefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mListData.clear();
                loadDatas();
            }
        });
//        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//                page++;
//                loadDatas();
//            }
//        }, recyclerView);
    }

    private void loadDatas() {
        Map<String, String> map = new HashMap<>();
        map.put("act", "send");
        map.put("page", String.valueOf(1));
        RetrofitManager.ApiFunc(RetrofitManager.mApiService.getMySendLove(map), new SubscriberListener<BaseResponse<LoveSendBean>>() {
            @Override
            public void onSuccess(BaseResponse<LoveSendBean> listBaseResponse) {
                List<LoveSendBean.UserInfoBean> data = listBaseResponse.data.getUserInfo();
                if (page == 1) {
                    mRefreshlayout.setRefreshing(false);
                }
                if (data == null || data.size() == 0) {
                    mAdapter.loadMoreEnd();
                    return;
                }
                mListData.addAll(data);
//                mListData.addAll(data);
                mAdapter.notifyDataSetChanged();
                mAdapter.loadMoreComplete();
            }

            @Override
            public void onFailer(String msg) {
                mRefreshlayout.setRefreshing(false);
            }

            @Override
            public void onTokenError() {
                mRefreshlayout.setRefreshing(false);
            }
        });
    }
}
